/*
 * @Author: 李琪 liqi@lark2game.com
 * @Date: 2025-08-27 14:13:31
 * @LastEditors: 李琪 liqi@lark2game.com
 * @LastEditTime: 2025-08-27 14:13:51
 * @FilePath: /gameWorld/assets/script/porline/CloudGenerator.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { _decorator, Component, Node, Sprite, SpriteFrame, UITransform, Vec2 } from 'cc';
import { PerlinNoise } from './PerlinNoise';

const { ccclass, property } = _decorator;

@ccclass('CloudGenerator')
export class CloudGenerator extends Component {
    @property({ type: Number })
    cloudWidth: number = 100;

    @property({ type: Number })
    cloudHeight: number = 50;

    @property({ type: Number })
    scale: number = 15;

    @property({ type: Number })
    seed: number = Math.random();

    @property({ type: Number })
    cloudDensity: number = 0.6; // 云层密度阈值

    @property({ type: Number })
    moveSpeed: number = 0.5; // 云层移动速度

    @property({ type: SpriteFrame })
    cloudTexture: SpriteFrame | null = null;

    private perlin: PerlinNoise = null!;
    private cloudTiles: Node[][] = [];
    private time: number = 0;

    onLoad() {
        this.perlin = new PerlinNoise(this.seed);
        this.generateClouds();
    }

    generateClouds() {
        // 清除现有云层
        this.node.removeAllChildren();
        this.cloudTiles = [];

        // 生成新云层
        for (let y = 0; y < this.cloudHeight; y++) {
            this.cloudTiles[y] = [];
            for (let x = 0; x < this.cloudWidth; x++) {
                // 使用柏林噪声生成云密度
                const noiseValue = this.perlin.noise(
                    x / this.scale,
                    y / this.scale
                );

                // 根据噪声值确定是否有云
                if (noiseValue > this.cloudDensity) {
                    const cloudNode = new Node(`Cloud_${x}_${y}`);
                    const sprite = cloudNode.addComponent(Sprite);
                    const transform = cloudNode.addComponent(UITransform);

                    transform.setContentSize(32, 32);
                    cloudNode.setPosition(x * 32, y * 32);

                    // 设置云纹理
                    sprite.spriteFrame = this.cloudTexture;
                    // 设置透明度基于噪声值
                    sprite.color.set(sprite.color.r, sprite.color.g, sprite.color.b,
                        (noiseValue - this.cloudDensity) / (1 - this.cloudDensity));

                    this.node.addChild(cloudNode);
                    this.cloudTiles[y][x] = cloudNode;
                }
            }
        }
    }

    update(deltaTime: number) {
        this.time += deltaTime;

        // 移动云层
        for (let y = 0; y < this.cloudHeight; y++) {
            for (let x = 0; x < this.cloudWidth; x++) {
                if (this.cloudTiles[y] && this.cloudTiles[y][x]) {
                    const cloudNode = this.cloudTiles[y][x];
                    // 使用噪声创建自然移动效果
                    const offsetX = this.perlin.noise(x * 0.1, this.time * 0.1) * this.moveSpeed;
                    const offsetY = this.perlin.noise(y * 0.1, this.time * 0.1 + 100) * this.moveSpeed * 0.5;

                    cloudNode.setPosition(
                        x * 32 + offsetX,
                        y * 32 + offsetY
                    );
                }
            }
        }
    }
}